package com.cuit.dashboard.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.dashboard.common.util.PageUtil;
import com.cuit.dashboard.system.dao.SysNoticeDao;
import com.cuit.dashboard.system.entity.SysNoticeEntity;
import com.cuit.dashboard.system.service.SysNoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @program: zuke
 * @description: 系统公告服务实现
 * @author: KamTang
 * @create: 2022-04-23 13:32
 **/
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeDao, SysNoticeEntity> implements SysNoticeService {
    /**
     * 分页查询
     *
     * @param params 传入参数
     * @return PageUtil
     */
    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        System.out.println(params);
        String title = (String) params.get("noticeTitle");
        String createBy = (String) params.get("createBy");
        String type = (String) params.get("type");
        String status = (String) params.get("status");
        System.out.println(status);
        long currentPage = Long.parseLong((String) params.get("currentPage"));
        long pageSize = Long.parseLong((String) params.get("pageSize"));
        QueryWrapper<SysNoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(createBy), "create_by", createBy)
                .like(StringUtils.isNotBlank(title), "title", title)
                .eq(StringUtils.isNotBlank(type), "type", Integer.getInteger(type))
                .eq(StringUtils.isNotBlank(status),"status", 0);
        Page<SysNoticeEntity> page = new Page<>(currentPage, pageSize);
        Page<SysNoticeEntity> selectPage = this.baseMapper.selectPage(page, wrapper);
        List<SysNoticeEntity> noticeList = selectPage.getRecords();
        int total = (int) selectPage.getTotal();
        return new PageUtil(noticeList, total, (int) pageSize, (int) currentPage);
    }

    @Override
    public PageUtil queryPageBySql(Map<String, Object> params) {
        int currentPage = 0, pageSize = 0;
        Object currentPageObj = params.get("currentPage");
        Object pageSizeObj = params.get("pageSize");
        if (currentPageObj == null || pageSizeObj == null) {
            params.put("currentPage", null);
            params.put("pageSize", null);
        } else {
            params.put("status", Integer.parseInt((String) params.get("status")));
            currentPage = Integer.parseInt((String) currentPageObj);
            pageSize = Integer.parseInt((String) pageSizeObj);
            params.put("currentPage", currentPage - 1);
        }
        List<SysNoticeEntity> noticeEntityList = this.baseMapper.queryByPage(params);
        int size = noticeEntityList.size();
        return new PageUtil(noticeEntityList, size, currentPage, pageSize);
    }
}
